const mongoose = require('mongoose');

const SiteSchema = new mongoose.Schema({
  user: {
    type: mongoose.Schema.ObjectId,
    ref: 'User',
    required: true
  },
  url: {
    type: String,
    required: [true, '请输入网站URL'],
    unique: true,
    match: [
      /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/,
      '请输入有效的URL'
    ]
  },
  name: {
    type: String,
    required: [true, '请输入网站名称'],
    maxlength: [50, '网站名称不能超过50个字符']
  },
  stats: {
    pagesIndexed: {
      type: Number,
      default: 0
    },
    backlinks: {
      type: Number,
      default: 0
    },
    domainAuthority: {
      type: Number,
      default: 0
    },
    pageSpeed: {
      type: Number,
      default: 0
    }
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: {
    type: Date,
    default: Date.now
  }
});

// 更新updatedAt字段
SiteSchema.pre('save', function(next) {
  this.updatedAt = Date.now();
  next();
});

module.exports = mongoose.model('Site', SiteSchema);